﻿using System;
using System.Data;
using System.Diagnostics;
using System.Configuration;

using contSys.database.DAL;
using contSys.Security;

namespace contSys.BL.Simulator
{
    public class Units
    {
        private static string _connString = ConfigurationManager.ConnectionStrings["Simulator"].ConnectionString;
        private static string tableObjName = "Units";

        public static string LastErrorMsg = string.Empty;

        /// <summary>
        /// get all units
        /// </summary>
        /// <returns>DataSet of all units or null</returns>
        public static DataSet GetAllUnits(string usr, string pwd)
        {
            if (UserManager.QueryAllowed(tableObjName, usr, pwd))
            {
                LastErrorMsg = string.Empty;
                string sql = "SELECT * FROM Units";
                return SQLiteDB.GetRecords(_connString, sql);
            }
            else
            {
                LastErrorMsg = "Failed security";
            }
            return null;
        }

        /// <summary>
        /// get unit by UnitID
        /// </summary>
        /// <param name="unitID"></param>
        /// <returns>Dataset of unit or null</returns>
        public static DataSet GetUnitByID(string usr, string pwd, object unitID)
        {
            if (UserManager.QueryAllowed(tableObjName, usr, pwd))
            {
                LastErrorMsg = string.Empty;
                string sql = string.Format("SELECT * FROM Units WHERE UnitID = {0}", unitID.ToString());
                return SQLiteDB.GetRecords(_connString, sql);
            }
            else
            {
                LastErrorMsg = "Failed security";
            }
            return null;
        }

        /// <summary>
        /// get units by where expression.
        /// do not add the 'WHERE' itself...
        /// </summary>
        /// <param name="whereExpr"></param>
        /// <returns>DataSet of units or null</returns>
        public static DataSet GetUnitByExpr(string usr, string pwd, string whereExpr)
        {
            if (UserManager.QueryAllowed(tableObjName, usr, pwd))
            {
                LastErrorMsg = string.Empty;
                string sql = string.Format("SELECT * FROM Units WHERE ({0})", whereExpr);
                return SQLiteDB.GetRecords(_connString, sql);
            }
            else
            {
                LastErrorMsg = "Failed security";
            }
            return null;
        }

        /// <summary>
        /// Adds a new unit
        /// </summary>
        /// <param name="UnitID"></param>
        /// <param name="Address"></param>
        /// <param name="Name"></param>
        /// <returns>true/false</returns>
        public static bool AddUnit(string usr, string pwd, object UnitID, object Address, string Name)
        {
            if (UserManager.AddAllowed(tableObjName, usr, pwd))
            {
                LastErrorMsg = string.Empty;
                string sql = string.Format("INSERT INTO Units (UnitID, Address, Name) VALUES ({0},{1},'{2}')", UnitID.ToString(), Address.ToString(), Name);
                return SQLiteDB.TryExecuteScalar(_connString, sql);
            }
            else
            {
                LastErrorMsg = "Failed security";
            }
            return false;
        }

        /// <summary>
        /// modifies an existing unit
        /// </summary>
        /// <param name="usr"></param>
        /// <param name="pwd"></param>
        /// <param name="unitID"></param>
        /// <param name="UnitID"></param>
        /// <param name="Address"></param>
        /// <param name="Name"></param>
        /// <returns>true/false</returns>
        public static bool ModifyUnit(string usr, string pwd, object unitID, object Address, string Name)
        {
            if (UserManager.ModifyAllowed(tableObjName, usr, pwd))
            {
                LastErrorMsg = string.Empty;
                string sql = string.Format("UPDATE Units SET Address={0}, name='{1}' WHERE (UnitID={2})", Address.ToString(), Name, unitID.ToString());
                return SQLiteDB.TryExecuteScalar(_connString, sql);
            }
            else
            {
                LastErrorMsg = "Failed security";
            }
            return false;
        }

        public static bool DeleteUnit(string usr, string pwd, object unitID)
        {
            if (UserManager.DeleteAllowed(tableObjName, usr, pwd))
            {
                LastErrorMsg = string.Empty;
                return SQLiteDB.TryExecuteScalar(_connString, "DELETE FROM Units WHERE UnitID=" + unitID);
            }
            else
            {
                LastErrorMsg = "Failed security";
            }
            return false;
        }

        private static void Writelog(string msg)
        {
            const string cs = "contSys.BL.Simulator";

            EventLog elog = new EventLog();
            if (!EventLog.SourceExists(cs))
                EventLog.CreateEventSource(cs, cs);

            elog.Source = cs;
            elog.EnableRaisingEvents = true;
            elog.WriteEntry(msg);
        }
    }
}
